Plotly and Cufflinks

Plotly is a library that allows you to create interactive plots.

Installation

pip install plotly
pip install cufflinks

Imports and Set-up


In [1]:
import pandas as pd
import numpy as np
%matplotlib inline

In [2]:
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot

print(__version__) # requires version >= 1.9.0


1.12.9

In [3]:
import cufflinks as cf

In [4]:
# For Notebooks
init_notebook_mode(connected=True)



In [5]:
# For offline use
cf.go_offline()


Data (not real)


In [6]:
df = pd.DataFrame(np.random.randn(100,4),columns='A B C D'.split())

In [7]:
df.head()


Out[7]:
A B C D
0 0.250546 -0.126814 0.263805 -0.961964
1 -1.336465 1.253691 1.154073 -0.510275
2 0.480613 -0.939239 0.341656 -1.089402
3 -0.307764 1.423019 -1.205179 2.658885
4 0.575849 0.357668 -2.791770 -0.540994

In [8]:
df2 = pd.DataFrame({'Category':['A','B','C'],'Values':[32,43,50]})

In [9]:
df2.head()


Out[9]:
Category Values
0 A 32
1 B 43
2 C 50

Scatter


In [10]:
df.iplot(kind='scatter',x='A',y='B',mode='markers',size=10)


Bar Plots


In [11]:
df2.iplot(kind='bar',x='Category',y='Values')



In [12]:
df.count().iplot(kind='bar')


Boxplots


In [13]:
df.iplot(kind='box')


3d Surface


In [14]:
df3 = pd.DataFrame({'x':[1,2,3,4,5],'y':[10,20,30,20,10],'z':[5,4,3,2,1]})
df3.iplot(kind='surface',colorscale='rdylbu')


Spread


In [15]:
df[['A','B']].iplot(kind='spread')


histogram


In [16]:
df['A'].iplot(kind='hist',bins=25)



In [17]:
df.iplot(kind='bubble',x='A',y='B',size='C')


scatter_matrix()

Similar to sns.pairplot()


In [18]:
df.scatter_matrix()